Branchwell

Lumen & Gesso / website · lumengesso.com / app / api / orders / [id] / summary / route.ts

1import { NextResponse } from 'next/server';
2import { commerceDb } from '@/app/lib/commerce-db';
3import { getCustomerUser } from '@/app/lib/lumen-auth';
4
5export const runtime = 'nodejs';
6export const dynamic = 'force-dynamic';
7
8// Read-only summary of an UNPAID order, keyed by the order id. The id is
9// an unguessable capability token (lg_ + 10 chars from a 31-char
10// alphabet), so this mirrors the trust model of the resume-payment POST
11// (/api/orders/[id]/checkout): both run BEFORE the customer logs in,
12// because the magic link is deferred to the Stripe webhook. We expose
13// only pending_payment / abandoned orders so a leaked id can never
14// reveal a paid order's photos or details.
15//
16// Powers two screens that both arrive without the in-memory draft:
17// • the post-cancel / reminder resume screen (/checkout), which shows
18// the saved photos + design instead of a bare button, and
19// • the builder's "make changes" rehydration (/builder?edit=…), which
20// reconstructs the design + photos so nothing has to be re-uploaded.
21export async function GET(
22 request: Request,
23 { params }: { params: Promise<{ id: string }> },
24) {
25 const { id } = await params;
26 const admin = commerceDb();
27 if (new URL(request.url).searchParams.has('replace')) {
28 const user = await getCustomerUser();
29 if (!user) return NextResponse.json({ error: 'Sign in to start a replacement.' }, { status: 401 });
30 const { data: saved } = await admin.from('orders').select('user_email, config, pet_photo_urls, custom_style_url')
31 .eq('id', id).eq('user_id', user.id).eq('status', 'cancelled').maybeSingle();
32 if (!saved) return NextResponse.json({ error: 'Cancelled order not found.' }, { status: 404 });
33 const config = saved.config && typeof saved.config === 'object' && !Array.isArray(saved.config) ? saved.config : {};
34 const designFields = ['styleId', 'productId', 'size', 'orientation', 'frame', 'matteId', 'wrap', 'bgColorId', 'bgCustom', 'fontId', 'includeName', 'artistNote', 'pets'];
35 const assetPath = `/api/orders/photos?order=${encodeURIComponent(id)}&slot=`;
36 return NextResponse.json({
37 email: saved.user_email,
38 config: Object.fromEntries(Object.entries(config).filter(([key]) => designFields.includes(key))),
39 petPhotoUrls: (saved.pet_photo_urls ?? []).map((_: string, slot: number) => assetPath + slot),
40 customStyleUrl: saved.custom_style_url ? assetPath + 'style' : null,
41 }, { headers: { 'Cache-Control': 'private, no-store' } });
42 }
43 const { data: order, error } = await admin
44 .from('orders')
45 .select('status, user_email, config, pet_photo_urls, custom_style_url, price_cents')
46 .eq('id', id)
47 .single();
48
49 if (error || !order) {
50 return NextResponse.json({ error: 'order not found' }, { status: 404 });
51 }
52 if (order.status !== 'pending_payment' && order.status !== 'abandoned') {
53 return NextResponse.json({ error: 'order not resumable' }, { status: 409 });
54 }
55
56 return NextResponse.json({
57 status: order.status,
58 // Returned so the builder can prefill the email on the "make changes"
59 // flow — the customer already gave it when the order was created, and
60 // re-typing it to re-pay is friction. Same exposure class as the
61 // pet photos this endpoint already returns, gated by the same
62 // unguessable order-id capability token.
63 email: order.user_email ?? '',
64 config: order.config ?? {},
65 petPhotoUrls: order.pet_photo_urls ?? [],
66 customStyleUrl: order.custom_style_url ?? null,
67 priceCents: order.price_cents ?? 0,
68 });
69}